package com.sto.stoapp.thread;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.http.HttpManager;
import com.sto.stoapp.http.URLActions;
import com.sto.stoapp.http.request.SystemSettingSynchRequest;
import com.sto.stoapp.http.response.SystemSettingSynchResponse;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.Helper;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

/**
 * 系统功能设置同步
 * 
 * @author Administrator
 * 
 */
// {"respCode":"000","resMessage":"成功","sessionId":"22a344222fdf454b8eecd99e9f4f8b68","tid":"520131425211111","mty":"1856","opOrgCode":"200000","trace":"324000017","serverIp":"192.168.1.11","serverPort":"6388","scanUploadTime":"15","expressQueryOnoff":"1","gpsOnoff":"1","gpsUploadTime":"10","orderDownloadTime":"5","versionNo":"1426752012427"}
public class SystemSettingSynchThread extends BaseThread {
	Context context;
	Handler handler;

	public SystemSettingSynchThread(Context context, Handler handler) {
		super(context);
		this.context = context;
		this.handler = handler;
	}

	@Override
	public void run() {
		super.run();

		HttpManager mgr = new HttpManager();

		String json = new SystemSettingSynchRequest(context)
				.buildToRequestString();
		String response = mgr.doPostClient(json,
				StoFilePreference.getInstance(context).getServerIp()
						+ URLActions.SYSSYN_ACTION);

		Gson gson = new Gson();
		SystemSettingSynchResponse synchResponse = null;
		try {
			synchResponse = gson.fromJson(response,
					new TypeToken<SystemSettingSynchResponse>() {
					}.getType());
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
			ExceptionLog.writeLog("SystemSettingSynchThread", "系统功能同步失败 发送："
					+ json);
			ExceptionLog.writeLog("SystemSettingSynchThread", "系统功能同步失败 收到："
					+ response);
			return;
		}
		try {
			if (synchResponse != null) {
				// 快件查询
				if ("1".equals(synchResponse.expressQueryOnoff))
					StoFilePreference.getInstance(context)
							.setExpressSearchControl(true);
				else if ("0".equals(synchResponse.expressQueryOnoff))
					StoFilePreference.getInstance(context)
							.setExpressSearchControl(false);

				// 订单下载时间间隔
				if (!TextUtils.isEmpty(synchResponse.orderDownloadTime)
						&& Integer.parseInt(synchResponse.orderDownloadTime) > 5)
					StoFilePreference
							.getInstance(context)
							.setAutoDownloadOrderTime(
									Integer.parseInt(synchResponse.orderDownloadTime));
				// gps开关
				if ("1".equals(synchResponse.gpsOnoff))
					StoFilePreference.getInstance(context).setGpsSwitch(true);
				else if ("0".equals(synchResponse.gpsOnoff))
					StoFilePreference.getInstance(context).setGpsSwitch(false);
				// gps上传时间
				if (!TextUtils.isEmpty(synchResponse.gpsUploadTime)) {
					StoFilePreference.getInstance(context).setGpsAutoTime(
							synchResponse.gpsUploadTime);
				}
				// 扫描数据上传时间
				if (!TextUtils.isEmpty(synchResponse.scanUploadTime)) {
					StoFilePreference.getInstance(context).setAutoUploadTime(
							Integer.parseInt(synchResponse.scanUploadTime));
				}
				// IP
				if (!TextUtils.isEmpty(synchResponse.serverIp)
						&& !TextUtils.isEmpty(synchResponse.serverPort)) {
					// "http://116.228.73.26:17070/receive/"
					String server = "http://"+synchResponse.serverIp + ":"
							+ synchResponse.serverPort + "/receive/";
					StoFilePreference.getInstance(context).setServerIp(server);
					StoFilePreference.getInstance(context).setDataServicePort(
							Integer.parseInt(synchResponse.serverPort));
				}

				handler.post(new Runnable() {

					@Override
					public void run() {
						Helper.showToast(context, "系统功能同步成功");
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
			ExceptionLog.writeLog("SystemSettingSynchThread", e.toString());
			ExceptionLog.writeLog("SystemSettingSynchThread", "返回数据：response");
		}

	}

}
